
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-5589-1" />
	<title>Subir archivos</title>
	<style type="text/css">
	<!--background: transparent url("files/bg_legend.gif") no-repeat;-->
	<!--
	body {
		background-color: #DBE8F9;
		font: 11px/24px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
		color: #5A698B;
		padding-left:20px;
		overflow:hidden;
	}

	#title {
		width: 450px;
		height: 26px;
		color: #5A698B;
		font: bold 11px/18px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
		padding-top: 5px;
		text-transform: uppercase;
		letter-spacing: 2px;
		text-align: left;
	}

	.input {
		background-color: #fff;
		font: 11px/14px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
		color: #5A698B;
		margin: 4px 0 5px 8px;
		padding: 1px;
		border: 1px solid #8595B2;
	}

	.textarea {
		border: 1px solid #8595B2;
		background-color: #fff;
		font: 11px/14px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
		color: #5A698B;
		margin: 4px 0 5px 8px;
	}

	.upload_message_success {
		padding:4px;
		background-color:#009900;
		border:1px solid #006600;
		color:#FFFFFF;
		margin-top:10px;
		margin-bottom:10px;
		overflow:hidden;
	}

	.upload_message_error {
		padding:4px;
		background-color:#CE0000;
		border:1px solid #990000;
		color:#FFFFFF;
		margin-top:10px;
		margin-bottom:10px;
		overflow:hidden;
	}
	-->
	</style>
	</head>
<body>
	<?php 
		error_reporting(E_ALL);
		session_start();
		
		require_once ("fb.php");
		require_once ("FirePHP.class.php");
		
		$estacionn = $_SESSION['nombre_estacion'];
		$estacion = $_SESSION['estacion'];
				
		//$Numero_estacion = $_SESSION['Numero_estacion'];?>

		<div id="title">&nbsp;SUBIR ARCHIVOS EN <?php echo "".$estacionn.""; ?></div>
		
		<?php ini_set("memory_limit", "200000000"); // para imagenes grandes y asi no tener problemas con la memoria 
			include "database.php";	
			// upload the file
			if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) 
			{
				// Archivos de tamano menor a 5 MB
				if ( ($_FILES["image_upload_box"]["type"] == "application/pdf" || 
						$_FILES["image_upload_box"]["type"] == "application/msword" ||
						$_FILES["image_upload_box"]["type"] == "application/vnd.ms-excel" ||
						$_FILES["image_upload_box"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
						$_FILES["image_upload_box"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
					&& ($_FILES["image_upload_box"]["size"] < 5242880))
				{
					$file =$_FILES["image_upload_box"]["name"];
					define ("FILEDIRECTORIO","../files/".$estacionn."/");		
					
					move_uploaded_file($_FILES['image_upload_box']['tmp_name'], FILEDIRECTORIO."/".$file."");
					
					if(!is_dir('../files/'.$estacionn.'/')) mkdir('../files/'.$estacionn.'/') or die('no se puede crear directorio');
					
					
					$con = pg_connect("user=".DB_USER." port=".DB_PORT." dbname=".DB_NAME." host=".DB_HOST." password=".DB_PASS)
							or die ("Could not connect to server\n");

					$file_name = "../files/".$estacionn."/".$file;
					$img = fopen($file_name,'r') or die("");
					$data = fread($img, filesize($file_name));
					$es_data = pg_escape_bytea($data);

					$query = "INSERT INTO hsig.estacion_imagen(estacion_id, nm_nombre, tipo_imagen, objeto) 
							  Values($estacion,'$file', 'E', '$es_data')";
					
					pg_set_client_encoding($con, "LATIN1");
					
					if (!pg_query($con, $query)) 
					{
						//echo '<p>';
						//echo "not inserted en base de datos";
						header("Location: uploadpdfrs.php?upload_message=No inserto en la Base de Datos&upload_message_type=error");
						fclose($img);
						pg_close($con);
						exit;
					}
					else
					{
						//$descpBox = $_GET["descpBox"];					
						header("Location: uploadpdfrs.php?upload_message=Archivo almacenado satisfactoriamente &upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
						exit;
					}
				}
				else
				{
					header("Location: uploadpdfrs.php?upload_message=Verificar que sea un archivo del tipo PDF, DOC, DOCX, XLS o XLSX y si es de tamano menor a 5 MB&upload_message_type=error");
					exit;
				}
			}
	
			if(isset($_REQUEST['upload_message']))
			{
		?>
			<div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
			<?php echo htmlentities($_REQUEST['upload_message']);?>
				</div>
			<?php }	?>

		<form action="php/uploadpdf.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" target="foo" onSubmit="window.open('', 'foo', 'width=650,height=60,status=no,resizable=no,scrollbars=no')" >
		<label><br/><b>&nbsp;&nbsp;Formatos permitidos: PDF, DOC, DOCX, XLS, XLSX</b></label>
		<br/>
		<input name="image_upload_box" type="file" class = "input" id="image_upload_box" size="48" />
        <input type="image" src="files/guardar.gif" alt="Guardar imagen" name="submit" align="right"/>
		<input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
	</form>
</body>
</html>